package gw.com.android.ui.trade;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.List;

import gw.com.android.model.DataManager;
import www.com.library.model.DataItemDetail;
import www.com.library.util.DeviceUtil;

/**
 * @author jett
 * @since 2018-05-07.
 */
public class ChangeProductAdapter extends BaseAdapter {

    private Context context;
    private List<DataItemDetail> products;

    ChangeProductAdapter(Context context, List<DataItemDetail> products) {
        this.context = context;
        this.products = products;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout llView = new LinearLayout(context);
        llView.setOrientation(LinearLayout.VERTICAL);

        DataItemDetail item = getItem(position);

        TextView tv = new TextView(context);
        tv.setGravity(Gravity.CENTER);
        tv.setTextSize(14);
        tv.setTextColor(context.getResources().getColor(R.color.color_b));
        tv.setText(DataManager.instance().getPrdName(item));

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DeviceUtil.instance().dip2px(38, context));
        llView.addView(tv, params);
        // 最后一条数据不要分割线
        if (products != null && products.size() - 1 > position) {
            View divider = new View(context);
            divider.setBackgroundColor(context.getResources().getColor(R.color.color_d));
            params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = DeviceUtil.instance().dip2px(10, context);
            params.rightMargin = DeviceUtil.instance().dip2px(10, context);
            llView.addView(divider, params);
        }
        return llView;
    }

    @Override
    public int getCount() {
        return products == null ? 0 : products.size();
    }

    @Override
    public DataItemDetail getItem(int position) {
        if (products == null || position < 0 || position >= products.size()) {
            return null;
        }
        return products.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

}
